package com.yu.crm.utils;

import cn.hutool.core.codec.Base64;
import com.yu.crm.service.impl.ImgService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;


public class FileBase64Util {

    private static final Logger logger = LoggerFactory.getLogger(FileBase64Util.class);



    public static String fileToBase64(String path) {
        String base64 = null;
        InputStream in = null;
        try {
            File file = new File(path);
            in = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            in.read(bytes);
            base64 = Base64.encode(bytes);
        } catch (Exception e) {
            logger.error("BASE64编码出现异常");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }

    /**
     * BASE64解码成File文件
     *
     * @param
     * @author
     * @return String
     * @description 将文件转base64字符 File转成编码成BASE64
     */


    public static File base64ToFile(String str, String fileName) {
        File savefile = null;
        savefile = new File(fileName);
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try {
            byte[] bytes = Base64.decode(str);
            fos = FileUtils.openOutputStream(savefile);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            logger.error("BASE64解码成File文件出现异常",e);
            return null;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return savefile;
    }

    /**
     * 文件上传服务器
     * @param multipartFile
     * @param
     * @return
     */
//    public static String  saveFile(MultipartFile multipartFile, String uuid) {
        public static String  saveFile(MultipartFile multipartFile) {
        // TODO Auto-generated method stub
        if (!multipartFile.isEmpty()) {
            //文件上传到的位置
            //String filepathString="/Users/xuxuxiao/Documents/upload/"+uuid+"/"+multipartFile.getOriginalFilename();
            String filepathString="D:\\测试1\\"+multipartFile.getOriginalFilename()+".jpeg";
            File newfileFile=new File(filepathString);
            if (!newfileFile.getParentFile().exists()) {
                newfileFile.getParentFile().mkdirs();
            }
            try {
                multipartFile.transferTo(newfileFile);
                return "D:\\测试1\\"+multipartFile.getOriginalFilename();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        return null;
    }

    /**
     * 获取临时文件路径
     * @author wangchunlan5
     * 2019年3月17日
     * @param lastPath
     * @return
     */
    public static String getRelativeFilePath(String lastPath){
        String classPath = FileBase64Util.class.getClassLoader().getResource("/").getPath();
        return new StringBuffer(classPath.substring(0, classPath.indexOf("WEB-INF"))).append("WEB-INF/images/").append(lastPath).toString();
    }
    /**
     * 获取当前工程的跟路径
     * @return
     */
    public static String getProjectPath (){
        //String classPath = FileBase64Util.class.getClassLoader().getResource("/").getPath();
        return "C:\\Users\\Administrator\\Desktop\\newTSP\\appServer_phase2\\src\\main\\webapp\\images";
    }

    /**
     * BASE64解码成MultipartFile文件
     * @param str
     * @return
     */
    public static MultipartFile base64ToMultipartFile(String str) {
//        String ss = fileToBase64("D:\\java\\img\\1.jpg");
//        System.out.println(ss);
        File file = base64ToFile(str, "123.jpg");
        FileInputStream input = null;
        try {
            input = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        MultipartFile multipartFile = null;
        try {
            multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return multipartFile;
    }

    public static void main(String[] args) {
        String s = fileToBase64("C:\\Users\\99608\\Desktop\\test\\a.png");
        System.out.println(s);
    }


}
